package com.origin.onlinesys.web.controllers.modules.account;

import com.origin.onlinesys.common.controllers.BaseController;
import com.origin.onlinesys.domain.modules.interviewsys.Account;
import com.origin.onlinesys.service.modules.user.impl.AccountServiceImpl;
import com.origin.onlinesys.web.utils.WebUtils;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by origin on 2015/1/22.
 * 账户操作实体类
 */
@RequestMapping(value = "/management/user")
@Controller
public class AccountController extends BaseController {

    @Autowired
    AccountServiceImpl accountService;

    @Autowired
    DefaultPasswordService passwordService;

    @RequestMapping(value = "/add")
    public @ResponseBody
    int insertAccount(@ModelAttribute Account account){
        String roleId = account.getRoleId();
        int id = Integer.parseInt(accountService.selectByPhoneNum(account.getPhone1()));
        if(id>0) return id;

        account.setAddTime(sdf.format(new Date()));
        //获取加密之后的密码
        String encryptPassword = passwordService.encryptPassword(account.getPassword());
        account.setPassword(encryptPassword);
        accountService.insertAccount(account);
        accountService.addUserRole(roleId,account.getId());
        return 0;
    }

    @RequestMapping(value = "/update")
    public @ResponseBody
    int updateAccount(@ModelAttribute Account account){
        accountService.updateAccount(account);
        accountService.updateUserRole(account.getRoleId(),account.getId());
        return 1;
    }

    @RequestMapping(value = "/userUpdate")
    public @ResponseBody
    int userUpdate(@ModelAttribute Account account){
        WebUtils.getAccount().setRealname(account.getRealname());
        WebUtils.getAccount().setPhone1(account.getPhone1());
        WebUtils.getAccount().setEmail(account.getEmail());
        WebUtils.getAccount().setAddress(account.getAddress());
        return accountService.updateAccount(account);
    }

    @RequestMapping(value = "/del")
    public @ResponseBody
    int delAccount(String id){
        return accountService.deleteAccount(id);
    }

    @RequestMapping(value = "/findById")
    public @ResponseBody
    Account findById(String id){
        return accountService.selectById(id);
    }

    @RequestMapping(value = "/findByPage")
    public @ResponseBody
    Map findByPage(String name,int pageNum){
        if(name != null)name = name.trim();
        return accountService.selectByPage(pageNum,pageSize,name,
                WebUtils.getAccount().getQueryZoneId());
    }


    @RequestMapping(value = "/findInfo")
    public @ResponseBody
    Account findInfo(String id){
        return accountService.selectInfo(id);
    }

    @RequestMapping(value = "/findItem")
    public @ResponseBody
    List<Account> findItem(){
        return accountService.selectItem();
    }

    @RequestMapping(value = "/userSetZone")
    public @ResponseBody
    String userSetZone(String id){
        WebUtils.getAccount().setQueryZoneId(id);
        return "1";
    }

    @RequestMapping(value = "/findUserByRole")
    public @ResponseBody
    List<Account> findUserByRole(){
        return accountService.selectUserByRole(WebUtils.getAccount().getQueryZoneId());
    }

    @RequestMapping(value = "/findUserByRoleAndJobState")
    public @ResponseBody
    List<Account> findUserByRoleAndJobState(){
        return accountService.selectUserByRoleAndJobState(WebUtils.getAccount().getQueryZoneId());
    }
}
